//
//  ImageEditorController.m
//  YRSideViewController
//
//  Created by xiabin on 15/2/5.
//  Copyright (c) 2015年 YueRuo. All rights reserved.
//

#import "ImageEditorController.h"


@interface ImageEditorController ()
{
    UIView *statusBarView;
}
@end

@implementation ImageEditorController

- (void) initNavStyle {
    statusBarView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_SIZE_WIDTH, 20)];
    statusBarView.backgroundColor=QGNavBG;
    [self.view addSubview:statusBarView];
    self.navigationController.navigationBar.backgroundColor = QGNavBG;
    self.navigationController.navigationBar.barTintColor = QGNavBG;
    
    
    UIButton* cancelBtn = [Utils NavButtonStyleNormalBLeft:YES NormalImg:@"back" AndHightLight:@"back"];
    [cancelBtn addTarget:self action:@selector(moveBack) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:cancelBtn];
    self.navigationItem.title = @"裁剪头像";
    
    UIButton *DoneButton = [[UIButton alloc] initWithFrame: CGRectMake(0, 0, 40.0f, 0.0f)];
    [DoneButton setTitle:@"确定" forState:UIControlStateNormal];
    [DoneButton setTitleColor:QGTextWhite forState:UIControlStateNormal];
    [DoneButton setTitleColor:QGTextGrayL1 forState:UIControlStateHighlighted];
    [DoneButton addTarget:self action:@selector(Done) forControlEvents:UIControlEventTouchUpInside];
    DoneButton.titleLabel.font = [UIFont systemFontOfSize: [Utils phoneFontL4]];
    UIBarButtonItem *DoneButtonItem = [[UIBarButtonItem alloc] initWithCustomView:DoneButton];
    self.navigationItem.rightBarButtonItem = DoneButtonItem;
    
}

- (void)moveBack {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNavStyle];
    self.view.backgroundColor = QGHeadCroperBG;
   
    // Do any additional setup after loading the view.
}

-(ImageEditorController*) initWithIMG:(UIImage*)image {
    self = [super init];
    int width = self.view.frame.size.width * 0.75;
    int height = width;
    _coper = [[ImageCropperView alloc] initWithFrame:CGRectMake((self.view.frame.size.width-width)/2, (self.view.frame.size.height-height)/2, width, height)];
    [_coper.layer setCornerRadius:width / 2];  //修改半径，实现头像的圆形化
    _coper.layer.borderWidth = 1.0;
    _coper.layer.borderColor = [UIColor whiteColor].CGColor;
    _coper.clipsToBounds = YES;
    [_coper setup];
    _coper.image = image;
    [self.view addSubview:_coper];
    [self.view bringSubviewToFront:statusBarView];
    return self;
}

- (void)Done
{
    [_coper finishCropping];
    if (_iscamera) {
        [self.navigationController popViewControllerAnimated:YES];
    } else {
        [self.navigationController dismissViewControllerAnimated:YES completion:nil];
        //[self.navigationController dismissModalViewControllerAnimated:YES];
    }
    //[self.navigationController popViewControllerAnimated:YES];
    if ([_callback respondsToSelector:@selector(UMAsyncJobDone:JobCookie:And:)]) {
        [_callback UMAsyncJobDone:SUCCESS JobCookie:9999 And:_coper.croppedImage];
    }
    //[self.navigationController dismissModalViewControllerAnimated:YES];
    //[self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
